<template>
    <section>
        <div style="margin-bottom:5px;">
            <div >{{parentNameShow}}下部门信息</div>
            <el-input placeholder="请输入内容" size="small" v-model="searchKey" style="width: 250px;">
                <el-button slot="append" size="small" icon="el-icon-search" @click="getDataList"></el-button>
            </el-input>
            <el-button type="primary" size="small" @click="openAddDialog()">新 建</el-button>
        </div>
        <el-table ref="multipleTable" :data="dataList" border tooltip-effect="dark" style="width: 100%"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" label="序号">
            </el-table-column>
            <el-table-column prop="name" label="名称">
            </el-table-column>
            <el-table-column prop="deptNo" label="部门编号">
            </el-table-column>
            <el-table-column prop="createdBy" label="创建人" width="120">
                <template slot-scope="scope">
                    <id-to-name :id="scope.row.createBy"
                                url="sys/org/common/getNameById/orgUserCommonService"></id-to-name>
                </template>
            </el-table-column>
            <el-table-column label="日期" width="180">
                <template slot-scope="scope">{{ scope.row.createTime }}</template>
            </el-table-column>
            <el-table-column prop="status" label="状态" width="120">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status=='1'" type="success">启用</el-tag>
                    <el-tag v-else type="danger">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button size="mini" type="primary" @click="openDialog(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="removeItem(scope.row)">删除</el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div style="text-align: right;background: rgb(239, 242, 247);border: 1px solid #E5E9F2;">
            <el-pagination style="line-height: 30px; height: 30px;" @size-change="handleSizeChange"
                           @current-change="handleCurrentChange" :current-page="currentPage"
                           :page-sizes="[10, 20, 30, 50]" :page-size="pageSize"
                           layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <el-dialog :title="dialogAddOrEdit" :visible="dialogFormVisibleAdd" :modal="dialogFormVisibleAdd"
                   :before-close="openAddDialogClose">
            <el-form label-width="80px" :rules="formAddRules" :model="formAdd" ref="formAdd">

                <el-form-item label="名称" prop="name">
                    <el-input v-model="formAdd.name"></el-input>
                </el-form-item>
                <el-form-item label="部门编号" prop="deptNo">
                    <el-input v-model="formAdd.deptNo"></el-input>
                </el-form-item>
                <el-form-item label="上级部门" prop="parentId">

                    <el-input v-model="formAdd.parentName" style="width:85%"></el-input>
                    <span class="select" @click="selectUserDialog">选择</span>
                </el-form-item>
                　
                <el-form-item label="状态">
                    <el-switch on-text="启用" off-text="禁用" v-model="formAdd.status"></el-switch>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitFormAdd('formAdd')">保存</el-button>
                    <el-button @click="openAddDialogClose">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <div v-if="userDialogVisible">
            <user-dialog :DialogVisible="userDialogVisible"
                         :params="{user: selectUser,orgType:orgTypeValue}"
                         @on-dialog-change="onDialogChange"
                         :only="true"
            >
            </user-dialog>
        </div>
    </section>

</template>
<style>

</style>
<script>
    import {AjaxByAll} from '@/api/api';
    import idToName from '@/views/common/IdToName.vue';
    import userDialog from '@/views/common/userDialog.vue'

    export default {
        name: "sysOrgDeptList",
        data() {

            return {
                dataList: [],
                multipleSelection: [],
                dialogFormVisibleAdd: false,
                labelPosition: 'left',
                formAdd: {
                    id: null,
                    name: null,
                    deptNo: null,
                    parentId: null,
                    parentName: null,
                    status: true,

                },
                defaultData: null,
                pageSize: 10,
                currentPage: 1,
                total: 0,
                dialogAddOrEdit: '',
                searchKey: null,
                parentName: null,
                parentNameShow:null,
                formAddRules: {

                    deptNo: [
                        {required: true, message: '请输入部门编码', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'}
                    ]
                },
                userDialogVisible: false,
                selectUser: [],
                orgTypeValue: 1
            }
        },

        methods: {
            openDialog(model) {

                if (model) {
                    let id = model.id;
                    this.dialogAddOrEdit = '编辑部门';
                    let urls = 'sys/org/dept/' + id;
                    let method = 'get';
                    let form = {};
                    AjaxByAll(method, urls, form).then(data => {
                        if (data.code === 200) {
                            this.formAdd = data.data;
                        } else {
                            this.$message.error(data.msg);
                        }
                    });
                }
                this.dialogFormVisibleAdd = true;
            },
            getParentInfo(parentId) {

                if (parentId) {

                    let urls = 'sys/org/dept/' + parentId;
                    let method = 'get';
                    let form = {};
                    AjaxByAll(method, urls, form).then(data => {
                        if (data.code === 200) {
                            this.parentName = data.data.name;
                            this.parentNameShow = data.data.name;
                        } else {
                            this.$message.error(data.msg);
                        }
                    });
                } else {
                    this.parentNameShow="根部门";
                }

            },
            openAddDialog() {
                this.dialogAddOrEdit = '新建部门';
                // 送出後
                this.formAdd = Object.assign(this.$data.formAdd, this.defaultData);
                this.formAdd.parentId = this.parentId;
                this.formAdd.parentName = this.parentName;
                this.dialogFormVisibleAdd = true;
            },
            openAddDialogClose() {

                this.dialogFormVisibleAdd = false;
            },
            getDataList() {
                let self = this;
                let form = {
                    currentPage: self.currentPage,
                    pageSize: self.pageSize,
                    keyword: self.searchKey,
                    parentId: self.parentId

                };
                let urls = 'sys/org/dept/query';
                AjaxByAll('post', urls, form).then(data => {
                    if (data.code === 200) {

                        self.total = data.data.total * 1;
                        self.dataList = data.data.records;
                    }

                    this.loading = false;
                });
            },
            submitFormAdd(formName) {

                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let form = this.formAdd;
                        let urls = 'sys/org/dept/';
                        let method = this.formAdd.id
                        == null ? 'post' : 'put';
                        AjaxByAll(method, urls, form).then(data => {
                            if (data.code === 200) {
                                this.formAdd.id = data.data.id;
                                this.$message({
                                    message: '操作成功',
                                    type: 'success'
                                });

                                this.getDataList();
                            } else {
                                this.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                            this.dialogFormVisibleAdd = !this.dialogFormVisibleAdd;
                        });


                    } else {
                        this.$message({
                            message: '校验失败',
                            type: 'error'
                        });
                    }
                });
            },
            removeItem(model) {
                this.$confirm('确认移除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    AjaxByAll('delete', 'sys/org/dept/' + model.id, null).then(data => {
                        if (data.code === 200) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                            this.getDataList();
                        } else {
                            this.$message({
                                message: '操作失败：' + data.msg,
                                type: 'error'
                            });
                        }

                    });
                }).catch(() => {

                });
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.getDataList();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.getDataList();
            },
            onDialogChange(val) {
                this.userDialogVisible = val.visible;
                if (val.users.length > 0) {
                    this.formAdd.parentId = val.users[0].id;
                    this.formAdd.parentName = val.users[0].name;
                } else {
                    this.formAdd.parentId = null;
                    this.formAdd.parentName =null;
                }
                this.selectUser = [];
            },
            selectUserDialog() {
                this.userDialogVisible = true;
                if (this.formAdd.parentId != null && this.formAdd.parentId != '') {
                    this.selectUser[0] = {
                        id: this.formAdd.parentId,
                        name: this.formAdd.parentName
                    };
                }
            }
        },
        created() {
            this.defaultData = JSON.parse(JSON.stringify(this.formAdd));
        },
        mounted: function () {
            this.getDataList();
            this.getParentInfo(this.parentId);
        }, components: {
            userDialog,
            idToName
        }, props: ['parentId']
    }
</script>
